const fetch = require("node-fetch");
const controller = new AbortController();

// 正常的http调用 第二个参数是请求头对象
fetch("https://jackniu81.github.io", { signal: controller.signal })
  .then((r) => r.json())
  .then((response) => {
    console.log(response);
  })
  .catch((err) => {
    if (err.name === "AbortError") {
      console.log("Fetch1 was aborted");
    } else {
      console.log("Error", err);
    }
  });

fetch("https://jackniu81.github.io", { signal: controller.signal })
  .then((r) => r.json())
  .then((response) => {
    console.log(response);
  })
  .catch((err) => {
    if (err.name === "AbortError") {
      console.log("Fetch2 was aborted");
    } else {
      console.log("Error", err);
    }
  });

// 需要取消请求时，调用：
controller.abort();
